// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/buffer/buffer.h>
#include <pollux/common/strings/string_view.h>

namespace kumo::pollux {
    /// This class allows converting StringViews into memory-owned StringViews such
    /// that either this StringViewBufferHolder holds the necessary memory after
    /// copying, or the StringView is self contained. The main purpose is provide
    /// the caller a safe way of storing StringViews from an external source while
    /// maintaining appropriate ownership to memory.
    class StringViewBufferHolder {
    public:
        explicit StringViewBufferHolder(pollux::memory::MemoryPool *pool)
            : pool_(pool) {
        }

        /// Return a copy of the StringView where the StringView is copied to this
        /// StringViewBufferHolder if the StringView is not inlined. std::string and
        /// melon::StringPiece are also copied to the internal buffers (see the
        /// specializations below).
        ///
        /// NOTE: Out of convenience, we allow different types to be passed in, but
        /// just don't store them, so that the client does need to have this check.
        template<typename T>
        T getOwnedValue(T value) {
            if constexpr (std::is_same_v<T, StringView>) {
                return getOwnedStringView(std::move(value));
            } else {
                return std::move(value);
            }
        }

        /// Specialization for std::string type.
        StringView getOwnedValue(const std::string &value) {
            return getOwnedStringView(value.data(), value.size());
        }

        /// Specialization for melon::StringPiece type.
        StringView getOwnedValue(melon::StringPiece value) {
            return getOwnedStringView(value.data(), value.size());
        }

        /// Get all the buffers used for storing memory used by StringViews returned
        /// by getOwnedValue.
        std::vector<BufferPtr> moveBuffers() {
            auto result = std::move(stringBuffers_);
            stringBuffers_.clear();
            return result;
        }

        /// Return a copy of the vector of buffer pointers used for storing memory
        /// used in StringViews returned by getOwnedValue.  The underlying buffer
        /// pointers remain shared between this holder and the resulting collection.
        std::vector<BufferPtr> buffers() const {
            return stringBuffers_;
        }

    private:
        StringView getOwnedStringView(StringView stringView);

        StringView getOwnedStringView(const char *data, int32_t size);

        static constexpr int32_t kInitialStringReservation{1024 * 8};

        std::vector<BufferPtr> stringBuffers_;
        pollux::memory::MemoryPool *pool_;
    };
} // namespace kumo::pollux
